core = {};
core.menuHandler = function (node) {
    if ($('#menu').tree('isLeaf', node.target)) {
        if ($('#content').tabs('exists', node.text)) {
            $('#content').tabs('select', node.text);
        } else {
            $('#content').tabs('add', {
                title: node.text,
                iconCls: node.attributes.iconCls,
                href: node.attributes.href,
                closable: true
            });
        }
    }
};

core.func = {
    del: function (url, callback) {
        $.messager.confirm('确认', '你确认要删除选中的记录吗？', function (r) {
            if (r) {
                $.ajax({
                           url: url,
                           type: 'delete',
                           success: function (data) {
                               if (callback) {
                                   callback.call(this, data);
                               }
                           },
                           error: function (req, resp, err) {
                               console.log(req, resp, err);
                           }
                       });
            }
        });
    },
    post: function (url, params, callback) {
        $.ajax({
                   url: url,
                   type: 'post',
                   data: params,
                   dataType: 'json',
                   success: function (data) {
                       if (callback) {
                           callback.call(this, data);
                       }
                   },
                   error: function (req, resp, err) {
                       console.log(req, resp, err);
                   }
               });
    },
    patch: function (url, params, callback) {
        $.ajax({
                   url: url,
                   type: 'patch',
                   data: params,
                   dataType: 'json',
                   success: function (data) {
                       if (callback) {
                           callback.call(this, data);
                       }
                   },
                   error: function (req, resp, err) {
                       console.log(req, resp, err);
                   }

               });
    }
}

core.boolFormatter = function (value) {
    if (value) {
        return "是";
    }
    return "否";
};

core.dateformatter = function (val, row) {
    if (val != null) {
        var date = new Date(val);
        return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
    }
};

//validate plugins

$.extend($.fn.validatebox.defaults.rules, {
    equals: {
        validator: function (value, param) {
            return value == $(param[0]).val();
        },
        message: '两次输入的不一致.'
    }
});

$(document).ready(function () {

    //主页面菜单
    $('#menu').tree({
                        url: 'assets/json/menu.json',
                        onClick: core.menuHandler
                    });

    $("#content").tabs({
                           border: false,
                           fit: true
                       });

});

